package com.milhouz.bomberrokon.factories;

import java.io.IOException;

import android.content.res.AssetManager;
import android.util.Log;

import com.milhouz.bomberrokon.model.MapModel;
import com.milhouz.bomberrokon.util.MapXmlReader;

/**
 * Factory manipulating Maps<br>
 * <ul>
 * <li>List available maps</li>
 * <li>Load a map for its xml description</li>
 * </ul>
 */
public class MapFactory {

	private static MapFactory factory = null;

	public static MapFactory getFactory() {
		if (MapFactory.factory == null) {
			MapFactory.factory = new MapFactory();
		}
		return MapFactory.factory;
	}

	/** List maps names from map directory */
	public String[] listMaps(AssetManager assetManager) {
		try {
			return assetManager.list(MapXmlReader.ASSETS_MAP_FOLDER);
		} catch (IOException e) {
			Log.e("bomber", "Unable to list the assets/maps folder.");
		}
		return null;
	}

	/** Get map data with the given name from its XML description */
	public MapModel loadMapModel(String mapName) {
		// Build the map model
		MapModel mapModel = new MapModel(mapName);
		// Xml parser of the map
		MapXmlReader xmlReader = new MapXmlReader(mapName, mapModel);
		xmlReader.parse();
		return mapModel;
	}

}
